function getAllUpdate(){ //获取所有的发货仓
    var sel = document.getElementById("dcName"); //页面发货仓下拉框数据
    $.ajax({
        url: 'https://'+ window.location.host +'/api/admin/abaseservice/area/getAllWarehouse',
        cache: false, //禁用缓存
        type: "POST",
        dataType:"json",
        data:{},
        success: function(obj){
            for(var i=0;i<obj.data.length;i++){
                var option = new Option(obj.data[i].warehouse_name,obj.data[i].warehouse_code);
                sel.options.add(option)
            }
            dataRendering(); //加载数据渲染
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });
}

function dataRendering() {
    var wayId = getQueryString("wayId");
    $.ajax({
        url: 'https://'+ window.location.host +'/api/admin/alogistics/getDispatchModeById.jhtml',
        cache: false, //禁用缓存
        type: "POST",
        data:{
            id:wayId
        },
        success: function(data){
            var sel = document.getElementById("dcName");
            for(var k=0;k<sel.length;k++){  //回显发货仓
                if(sel[k].value == data.data.warehouseCode) {
                    $("#dcName option[value='"+sel[k].value+"']").attr("selected","selected");
                }
            }
            var typeSel = document.getElementById("type");

            for(var j=0;j<typeSel.length;j++){ //回显类型
                if(data.data.transportType == 10){
                    var typeWay = document.getElementById("typeWay");
                    $(typeWay).html("");
                    var option4 = new Option("鑫方盛物流","40");
                    typeWay.options.add(option4);
                }

                if(data.data.transportType == 20){
                    var typeWay = document.getElementById("typeWay");
                    $(typeWay).html("");
                    var option = new Option("快递","10");
                    var option1 = new Option("快运","20");
                    var option2= new Option("专车包车","30");
                    typeWay.options.add(option);
                    typeWay.options.add(option1);
                    typeWay.options.add(option2);
                }
                if(typeSel[j].value == data.data.transportType) {
                    $("#type option[value='"+typeSel[j].value+"']").attr("selected","selected");
                }
            }
            var typeWaySel = document.getElementById("typeWay");
            if(data.data.transportModeType == 30){
                $("input[type='radio'][name='maxLong']").get(0).checked = true;
                $("input[type='radio'][name='maxWeight']").get(0).checked = true;
                $("input[type='radio'][name='maxVolume']").get(0).checked = true;
                $("#longLimit").attr("disabled","disabled");
                $("#wightLimit1").attr("disabled","disabled");
                $("#wightLimit2").attr("disabled","disabled");
                $("#volumeLimit").attr("disabled","disabled");

                $($("input[type='radio'][name='maxLong']").get(1)).attr("disabled", true);
                $($("input[type='radio'][name='maxWeight']").get(1)).attr("disabled", true);
                $($("input[type='radio'][name='maxVolume']").get(1)).attr("disabled", true);
            }else{
                if (data.data.edgeLimit == 10){ //回显 最长边限制
                    $("input[type='radio'][name='maxLong']").get(1).checked = true;
                    $("#longLimit").val(data.data.maximumEdgeLimit)
                }else{
                    $("input[type='radio'][name='maxLong']").get(0).checked = true;
                    $("#longLimit").attr("disabled","disabled");
                }

                if (data.data.weightLimit == 10){ //回显重量限制
                    $("input[type='radio'][name='maxWeight']").get(1).checked = true;
                    $("#wightLimit1").val(data.data.minWeightLimit)
                    $("#wightLimit2").val(data.data.maxWeightLimit)

                }else{
                    $("input[type='radio'][name='maxWeight']").get(0).checked = true;
                    $("#wightLimit1").attr("disabled","disabled");
                    $("#wightLimit2").attr("disabled","disabled");
                }

                if (data.data.volumeLimit == 10){ //回显体积限制
                    $("input[type='radio'][name='maxVolume']").get(1).checked = true;
                    $("#volumeLimit").val(data.data.volumeWeight)
                }else{
                    $("input[type='radio'][name='maxVolume']").get(0).checked = true;
                    $("#volumeLimit").attr("disabled","disabled");
                }
            }
            for(var i=0;i<typeWaySel.length;i++){ //回显运送方式
                if(typeWaySel[i].value == data.data.transportModeType) {
                    $("#typeWay option[value='"+typeWaySel[i].value+"']").attr("selected","selected");
                }
            }



            if (data.data.transportStatus == 10){ //回显状态
                $("input[type='radio'][name='status']").get(0).checked = true;
            }else{
                $("input[type='radio'][name='status']").get(1).checked = true;
            }

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });

    
}

function wayUpdate() {
    var loginData=JSON.parse(web.readCookie("loginData"));
    var  warehouseCode = $('#dcName option:selected').val();
    var warehouseName = $('#dcName option:selected').text();
    var transportType = $('#type option:selected').val();
    var transportModeType = $('#typeWay option:selected').val();
    var edgeLimit= $("input:radio[name='maxLong']:checked").val();
    var maximumEdgeLimit = $("#longLimit").val();
    var weightLimit = $("input:radio[name='maxWeight']:checked").val();
    var minWeightLimit = $("#wightLimit1").val();
    var maxWeightLimit = $("#wightLimit2").val();
    var volumeLimit = $("input:radio[name='maxVolume']:checked").val();
    var volumeWeight = $("#volumeLimit").val();
    var transportStatus = $("input:radio[name='status']:checked").val();
    var createUser  = loginData.user.name;
    var wayId = getQueryString("wayId");
    var datas = {
        id:wayId,
        warehouseCode: warehouseCode,
        warehouseName: warehouseName,
        transportType: transportType,
        transportModeType: transportModeType,
        edgeLimit: edgeLimit,
        maximumEdgeLimit: maximumEdgeLimit,
        weightLimit: weightLimit,
        minWeightLimit: minWeightLimit,
        maxWeightLimit: maxWeightLimit,
        volumeLimit: volumeLimit,
        volumeWeight: volumeWeight,
        transportStatus: transportStatus,
        createUser: createUser
    }

    if (20 == transportStatus){
        //检查当前配送方式是否已经被模版引用
        var params = {
            warehouseCode: warehouseCode,
            transportModeType: transportModeType,
        }
        $.ajax({
            url: 'https://'+ window.location.host +'/api/admin/alogistics/findTemplateByWarehouseAndModetype.jhtml',
            cache: false, //禁用缓存
            type: "POST",
            dataType:'json',
            data: params,
            success: function(result){
                if(result.sub_code == 0){
                    var resultData = result.data;
                    if (null != resultData && resultData.length >0) {
                        layer.msg("配送方式已经被模版引用不能置为无效", {icon: 5,time:2000});
                    }else {
                        $.ajax({
                            url: 'https://'+ window.location.host +'/api/admin/alogistics/updateDispatchMode.jhtml',
                            cache: false, //禁用缓存
                            type: "POST",
                            dataType:'json',
                            data: datas,
                            success: function(result){
                                if(result.sub_code == 0){
                                    layer.msg(result.sub_msg, {
                                        icon: 1,
                                        time: 1000,
                                        end: function () {
                                            var index =  parent.location.reload();
                                            parent.layer.close(index);
                                        }
                                    });
                                }else{
                                    layer.msg(result.sub_msg, {icon: 5,time:2000});
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                console.log(XMLHttpRequest);
                                //alert(XMLHttpRequest);
                            }
                        });
                    }

                }else{
                    layer.msg(result.sub_msg, {icon: 5,time:2000});
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(XMLHttpRequest);
                //alert(XMLHttpRequest);
            }
        });
    }else {
        $.ajax({
            url: 'https://'+ window.location.host +'/api/admin/alogistics/updateDispatchMode.jhtml',
            cache: false, //禁用缓存
            type: "POST",
            dataType:'json',
            data: datas,
            success: function(result){
                if(result.sub_code == 0){
                    layer.msg(result.sub_msg, {
                        icon: 1,
                        time: 1000,
                        end: function () {
                            var index =  parent.location.reload();
                            parent.layer.close(index);
                        }
                    });
                }else{
                    layer.msg(result.sub_msg, {icon: 5,time:2000});
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(XMLHttpRequest);
                //alert(XMLHttpRequest);
            }
        });
    }




}

$("[name='maxLong']").change(function () { //长度为无限制的时候无法输入
    if(this.value == 20){
        $("#longLimit").val("")
        $("#longLimit").attr("disabled","disabled");
    }else{
        $("#longLimit").attr("disabled",false);
    }
})


$("[name='maxWeight']").change(function () { //重量为无限制的时候无法输入
    if(this.value == 20){
        $("#wightLimit1").val("");
        $("#wightLimit2").val("");
        $("#wightLimit1").attr("disabled","disabled");
        $("#wightLimit2").attr("disabled","disabled");
    }else{
        $("#wightLimit1").attr("disabled",false);
        $("#wightLimit2").attr("disabled",false);
    }
})

$("[name='maxVolume']").change(function () { //体积为无限制的时候无法输入
    if(this.value == 20){
        $("#volumeLimit").val("")
        $("#volumeLimit").attr("disabled","disabled");
    }else{
        $("#volumeLimit").attr("disabled",false);
    }
})


$("#type").change(function () {
    if($('#type option:selected').val() == 10){
        var typeWay = document.getElementById("typeWay");
        $(typeWay).html("");
        var option4 = new Option("鑫方盛物流","40");
        typeWay.options.add(option4);
    }

    if($('#type option:selected').val() == 20){
        var typeWay = document.getElementById("typeWay");
        $(typeWay).html("");
        var option = new Option("快递","10");
        var option1 = new Option("快运","20");
        var option2= new Option("专车包车","30");
        typeWay.options.add(option);
        typeWay.options.add(option1);
        typeWay.options.add(option2);
    }
})



$("#typeWay").change(function () {
    if($('#typeWay option:selected').val() == 30){
        $("input[type='radio'][name='maxLong']").get(0).checked = true;
        $("input[type='radio'][name='maxWeight']").get(0).checked = true;
        $("input[type='radio'][name='maxVolume']").get(0).checked = true;
        $("#longLimit").attr("disabled","disabled");
        $("#wightLimit1").attr("disabled","disabled");
        $("#wightLimit2").attr("disabled","disabled");
        $("#volumeLimit").attr("disabled","disabled");
        $("#longLimit").val("")
        $("#wightLimit1").val("")
        $("#wightLimit2").val("")
        $("#volumeLimit").val("")
        $($("input[type='radio'][name='maxLong']").get(1)).attr("disabled", true);
        $($("input[type='radio'][name='maxWeight']").get(1)).attr("disabled", true);
        $($("input[type='radio'][name='maxVolume']").get(1)).attr("disabled", true);
    }else{
        $($("input[type='radio'][name='maxLong']").get(1)).attr("disabled", false);
        $($("input[type='radio'][name='maxWeight']").get(1)).attr("disabled", false);
        $($("input[type='radio'][name='maxVolume']").get(1)).attr("disabled", false);
    }
})



function backList() {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}